package com.gacu.viewer.client.ui.menu;

import java.util.HashMap;

import com.gacu.viewer.client.algo.AbsAlgoWidget;
import com.gacu.viewer.client.algo.AlgoWidgetMaker;
import com.gacu.viewer.client.algo.match.MatchAlgoConfigWidget;
import com.gacu.viewer.client.algo.match.MatchAlgoWidgetMaker;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class MenuAlgo extends Composite{

	private VerticalPanel algosPanel;
	private ListBox lb;
	private int widgetWidth;
	
	private HashMap<String, AlgoWidgetMaker> algos;
	
	private static final String[] algoNames = {"Match Algorithm"};
	private static final AlgoWidgetMaker[] algoClasses = {new MatchAlgoWidgetMaker()};
	
	
	public MenuAlgo(int pxWidth)
	{
		super();
		
		VerticalPanel vp;
		
		vp = new VerticalPanel();
		widgetWidth = pxWidth - 2;
		
		algosPanel = new VerticalPanel();
		
		vp.setWidth(pxWidth + "px");
		
		algosPanel.setWidth("100%");
		
		algos = new HashMap<String, AlgoWidgetMaker>();
		for(int i=0;i<algoNames.length;i++)
		{
			algos.put(algoNames[i], algoClasses[i]);
		}
		
		vp.add(makeTopAlgos());
		
		vp.add(algosPanel);
		//vp.add(new MatchAlgoConfigWidget(pxWidth - 5));
		
		initWidget(vp);
	}

	private Widget makeTopAlgos() {
		VerticalPanel vp;
		HorizontalPanel hp;
		Button btn;
		
		vp = new VerticalPanel();
		
		hp = new HorizontalPanel();
		hp.add(new Label("Algo : "));
		lb = new ListBox();
		for(int i=0;i<algoNames.length;i++)
		{
			lb.addItem(algoNames[i]);
		}
		hp.add(lb);
		vp.add(hp);
		
		btn = new Button("Add algo");
		btn.addClickHandler(new AlgoAdderHandler());
		
		vp.add(btn);
		
		return vp;
	}
	
	private class AlgoAdderHandler implements ClickHandler
	{
		@Override
		public void onClick(ClickEvent event) {
			AbsAlgoWidget w;
			
			w = algos.get(lb.getValue(lb.getSelectedIndex())).getWidget(widgetWidth);
			
			algosPanel.add(w);
			
			w.showOptions();
		}
		
	}
	
	
}










